<?php
/**
 * Created by PhpStorm.
 * User: 会吃草的鱼
 * Date: 16/8/9
 * Time: 10:18
 */

namespace Recruit\Api;


use Common\Api\Api;
use Recruit\Model\CompanyApplyModel;

class CompanyApplyApi extends Api{
    /*
     * 取回一条数据 根据ID
     * */
    const GET_INFO = "Recruit/CompanyApply/getInfo";
    const JION_PAGE = 'Recruit/CompanyApply/joinpage';

    protected function _init(){
        $this->model = new CompanyApplyModel();
    }

    /*
    * 连表分页查询 获得投向公司的简历
    *  @param 查询条件|null $map
    * @param array|分页参数 $page
    * @param bool|排序参数 $order
    * @param bool|点击分页时带参数 $params
    * @param bool $fields
    * @return array
    * @internal param 查询条件 $map
    * @internal param 分页参数 $page
    * @internal param 排序参数 $order
    * @internal param 点击分页时带参数 $params
    * */
    public function joinpage($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){

        $query = $this->model->join('RIGHT JOIN itboye_user_resume ON itboye_company_apply.resume_id = itboye_user_resume.id')->join('LEFT JOIN itboye_company_employ ON itboye_company_apply.company_employ_id = itboye_company_employ.id');
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }
}